package com.happypets.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.happypets.servicesImpl.UserServiceImpl;

public class DealOrderServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public DealOrderServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=UTF-8");
		response.setHeader("Cache-Control", "no-store"); 
		
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		
		UserServiceImpl us = new UserServiceImpl();
		
		Map<String,String[]> maps = request.getParameterMap();
		
			String userId = maps.get("userId")[0];
			String allPrice = maps.get("allPrice")[0];
			String name = maps.get("postName")[0];
			String address = maps.get("postAddress")[0];
			String phone = maps.get("postPhone")[0];
			String petId[] = maps.get("petId");
			String petPrices[] = maps.get("petPrice");
			String petCount[] = maps.get("petCount");
			
			List<Map<String,String>> ls = new ArrayList<Map<String,String>>();
			for (int i = 0; i < petCount.length; i++) {
				Map<String,String> m = new HashMap<String,String>();
				
				m.put("petId", petId[i]);
				m.put("petPrice", petPrices[i]);
				ls.add(m);
			}
			int f = us.formDeal(name,phone,address,userId, allPrice, ls);
			String s=null;
			if(f == 1){
				s="[{\"flag\":\"1\",\"msg\":\"添加成功\"}]";
				session.setAttribute("shopSuccess", "123");
				session.removeAttribute("shopSuccess");
			}else{
				s="[{\"flag\":\"0\",\"msg\":\"添加失败\"}]";
			}
			System.out.println(s);
			out.write(s);
			out.flush();
			out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
